/**
 * Copyright (C), 2015-2019, XXX有限公司
 * FileName: UserServiceImpl
 * Author:   zhouheng
 * Date:     2019/6/8 23:22
 * Description:
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.zhouh.golden.system.service.impl;

import com.zhouh.golden.common.service.impl.BaseService;
import com.zhouh.golden.system.dao.UserMapper;
import com.zhouh.golden.system.domain.MyUser;
import com.zhouh.golden.system.dto.UserQueryRequest;
import com.zhouh.golden.system.dto.UserQueryResponse;
import com.zhouh.golden.system.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

/**
 * 〈一句话功能简述〉<br> 
 * 〈〉
 *
 * @author zhouheng
 * @create 2019/6/8
 * @since 1.0.0
 */
@Service("userService")
public class UserServiceImpl extends BaseService<MyUser> implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public MyUser findByNameOrMobile(String username) {
        Example example = new Example(MyUser.class);
        boolean isMobile = isPhoneNo(username);
        if (isMobile) {
            example.createCriteria().andCondition("mobile=", username);
        }
        example.createCriteria().andCondition("lower(username)=", username.toLowerCase());
        List<MyUser> list = this.selectByExample(example);
        return list.isEmpty() ? null : list.get(0);
    }

    /**
     * @description: 是否手机号
     * @date: 2019/6/16 15:30
     * @author:zhouheng
     */
    private boolean isPhoneNo(String phoneNo) {
        String regex = "[1]\\d{10}";
        if (StringUtils.isBlank(phoneNo))
            return false;
        else
            return phoneNo.matches(regex);
    }

    @Override
    public MyUser findByName(String username) {
        Example example = new Example(MyUser.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andCondition("username = ",username.toLowerCase());
        List<MyUser> list = this.getMapper().selectByExample(example);
        return list.isEmpty() ? null : list.get(0);
    }

    @Override
    public MyUser findByMobile(String mobile) {
        Example example = new Example(MyUser.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andCondition("mobile = ", mobile);
        List<MyUser> list = this.getMapper().selectByExample(example);
        return list.isEmpty() ? null : list.get(0);
    }

    @Override
    public void registUser(MyUser user) {
        user.setCrateTime(new Date());
        user.setTheme(MyUser.DEFAULT_THEME);
        user.setAvatar(MyUser.DEFAULT_AVATAR);
        user.setSsex(MyUser.SEX_UNKNOW);
        user.setMobile(user.getMobile());
        this.save(user);
//        UserRole ur = new UserRole();
//        ur.setUserId(user.getUserId());
//        ur.setRoleId(2L);
//        this.userRoleMapper.insert(ur);
    }

    @Override
    public UserQueryResponse getUserById(UserQueryRequest userQueryRequest) {
        UserQueryResponse userQueryResponse = new UserQueryResponse();

        try {
            MyUser user = userMapper.selectByPrimaryKey(userQueryRequest.getUserId());
            if(user != null){
                userQueryResponse.setAvatar(user.getAvatar());
                userQueryResponse.setSex(user.getSsex());
                userQueryResponse.setUsername(user.getUsername());
                userQueryResponse.setMobile(user.getMobile());

                return userQueryResponse;
            }
        }catch (Exception e){

        }


        return null;
    }
}